package com.zibo.convalesce.common.result;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 通用结果返回方法
 * @param <T>
 */

public class AjaxResult<T> implements Serializable {

    private int code;

    private String msg;

    private T data;

    public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public static <T> AjaxResult<T> success() {
        return restResult(AjaxResultCode.SUCCESS.getCode(), AjaxResultCode.SUCCESS.getMsg(), null);
    }

    public static <T> AjaxResult<T> success(String msg, T data) {
        return restResult(AjaxResultCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> AjaxResult<T> success(T data) {
        return restResult(AjaxResultCode.SUCCESS.getCode(), AjaxResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> AjaxResult<T> success(String msg) {
        return restResult(AjaxResultCode.SUCCESS.getCode(), msg, null);
    }

    public static <T> AjaxResult<T> failed() {
        return restResult(AjaxResultCode.FAILED.getCode(), AjaxResultCode.FAILED.getMsg(), null);
    }

    public static <T> AjaxResult<T> failed(T data) {
        return restResult(AjaxResultCode.FAILED.getCode(), AjaxResultCode.FAILED.getMsg(), data);
    }

    public static <T> AjaxResult<T> failed(String msg) {
        return restResult(AjaxResultCode.FAILED.getCode(), msg, null);
    }

    public static <T> AjaxResult<T> failed(String msg, T data) {
        return restResult(AjaxResultCode.FAILED.getCode(), msg, data);
    }

    public static <T> AjaxResult<T> failed(int code, String msg, T data) {
        return restResult(code, msg, data);
    }

    public static <T> AjaxResult<T> failed(int code, String msg) {
        return restResult(code, msg, null);
    }

    private static <T> AjaxResult<T> restResult(int code, String msg, T data) {
        AjaxResult<T> apiResult = new AjaxResult<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
